using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PP.Basic
{
    /// <summary>
    /// 简单的淡入动画
    /// </summary>
    [RequireComponent(typeof(CanvasGroup))]
    public class SimpleFadeAnimation : MonoBehaviour
    {
        private const string Tag = nameof(SimpleFadeAnimation);

        // Private Variables Begin
        [Tooltip("动画时长")]
        [SerializeField]
        private float _duration = 0.3f;
        [Tooltip("淡出最小透明度")]
        [Range(0f, 1f)]
        [SerializeField]
        private float _fadeMin = 0f; 

        private bool _isInited;
        private bool _isFadingIn;
        private bool _isFadingOut;
        // Private Variables End

        // Public Methods Begin
        public void FadeIn()
        {
            if (!_isInited)
            {
                Debug.LogError($"{Tag}.FadeIn# Not inited");
                return;
            }

            if (_isFadingIn)
                return;
            _isFadingIn = true;
            _isFadingOut = false;

            // Debug.Log($"{Tag}.FadeIn# In");
            DOTween.Kill(gameObject);
            var fromAlpha = _canvasGroup.alpha;
            var toAlpha = 1.0f;
            var duration = _duration * (toAlpha - fromAlpha);
            var seq = DOTween.Sequence(gameObject);
            seq.Append(DOTween.To(() => fromAlpha, (float value) => _canvasGroup.alpha = value, toAlpha, duration));
        }

        public void FadeOut()
        {
            if (!_isInited)
            {
                Debug.LogError($"{Tag}.FadeOut# Not inited");
                return;
            }

            if (_isFadingOut)
                return;
            _isFadingOut = true;
            _isFadingIn = false;

            // Debug.Log($"{Tag}.FadeOut# In");
            DOTween.Kill(gameObject);
            var fromAlpha = _canvasGroup.alpha;
            var toAlpha = _fadeMin;
            var duration = _duration * (fromAlpha - toAlpha);
            var seq = DOTween.Sequence(gameObject);
            seq.Append(DOTween.To(() => fromAlpha, (float value) => _canvasGroup.alpha = value, toAlpha, duration));
        }
        // Public Methods End
        
        // Lifecycle Methods Begin
        private void Awake()
        {
            Initiate();
        }

        public void Initiate()
        {
            if (_isInited)
                return;
            _isInited = true;

            InitHier();
            InitPart();
        }
        
        private void InitPart()
        {
        }

        private void Start()
        {
            // StartCoroutine(RunTest());
        }

        private void OnDestroy()
        {
            DOTween.Kill(gameObject);
        }
        // Lifecycle Methods End

        // Hier Components Begin  
        private CanvasGroup _canvasGroup;

        private void InitHier()
        {
            _canvasGroup = transform.GetComponentEx<CanvasGroup>();
        }
        // Hier Components End

        // Event Methods Begin
        // Event Methods End

        // Private Methods Begin
        // Private Methods End

        // Test Methods Begin
        private IEnumerator RunTest()
        {
            if (!Application.isEditor)
                yield break;

            while (true)
            {
                yield return new WaitForSeconds(1f);
                FadeIn();
                yield return new WaitForSeconds(1f);
                FadeOut();
            }
        }
        // Test Methods End
    }
}
